package com.coin.exchanges.utils

import android.content.Context
import android.graphics.*
import android.support.v7.widget.AppCompatEditText
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import com.coin.exchanges.R


class ClearEditText : AppCompatEditText {
    private var mClearBitmap: Bitmap? = null
    private var mPaint: Paint? = null
    private var mWidth: Int = 0
    private var mHeight: Int = 0
    private var mBitWidth: Int = 0
    private var mBitHeight: Int = 0
    private var showClose: Boolean = false
    private var scale: Float = 0.toFloat()
    private var padding: Float = 0.toFloat()
    private var mDrawWidth: Float = 0.toFloat()

    private var hasScale: Boolean = false

    constructor(context: Context) : super(context) {
        init(context, null)
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init(context, attrs)
    }

    private fun init(context: Context, attrs: AttributeSet?) {
        var clearIcon = 0
        if (attrs != null) {
            //获得这个控件对应的属性。
            val a = getContext().obtainStyledAttributes(attrs, R.styleable.ClearEditText)
            try {
                //获得属性值
                clearIcon = a.getResourceId(R.styleable.ClearEditText_clearIcon, 0)
                scale = a.getFloat(R.styleable.ClearEditText_scaleSize, 0f)
                mDrawWidth = a.getDimension(R.styleable.ClearEditText_drawableWidth, 0.0f)
            } finally { //回收这个对象
                a.recycle()
            }
        }
        //设置删除图标
        val bfoOptions = BitmapFactory.Options()
        bfoOptions.inScaled = false
        mClearBitmap = if (clearIcon != 0) {
            BitmapFactory.decodeResource(resources, clearIcon, bfoOptions)
        } else {
            BitmapFactory.decodeResource(resources, R.mipmap.base_edit_text_del, bfoOptions)
        }

        if (scale == 0f) {
            scale = DEFAUT_SCALE
        }

        mPaint = Paint()

        addTextChangedListener(object : TextWatcher {
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                showClose = !TextUtils.isEmpty(s)
                invalidate()
            }

            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

            }

            override fun afterTextChanged(s: Editable) {}
        })

        onFocusChangeListener = OnFocusChangeListener { v, hasFocus ->
            showClose = if (hasFocus) {
                !TextUtils.isEmpty(text.toString())
            } else {
                false
            }
            invalidate()
        }
    }

    // 处理删除事件
    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (event.action == MotionEvent.ACTION_UP) {
            //在图标的范围内点击有效
            if (showClose && event.x > width - height + padding
                    && event.x < width - padding
                    && event.y > padding
                    && event.y < height - padding) {
                setText("")
            }
        }
        return super.onTouchEvent(event)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        if (showClose) {
            val mSrcRect = Rect(0, 0, mBitWidth, mBitWidth)
            val mDestRect = RectF(mWidth.toFloat() - mBitWidth.toFloat() - padding,
                    padding, mWidth - padding, mHeight - padding)
            canvas.drawBitmap(mClearBitmap!!, mSrcRect, mDestRect, mPaint)
        }
    }

    private fun deal() {
        if (!hasScale) {
            val width = mClearBitmap!!.width
            val height = mClearBitmap!!.height
            Log.e("HHHHH", "width height $width $height")
            // 设置想要的大小
            if (mDrawWidth == 0f) {
                padding = mHeight.toFloat() * (1 - scale) / 2
                mBitWidth = (mHeight - 2 * padding).toInt()
                mBitHeight = mBitWidth
            } else {
                if (mDrawWidth > mHeight) {
                    mDrawWidth = mHeight.toFloat()
                }
                padding = (mHeight - mDrawWidth) / 2
                mBitWidth = mDrawWidth.toInt()
                mBitHeight = mDrawWidth.toInt()
            }
            // 计算缩放比例
            val scaleWidth = mBitWidth.toFloat() / width
            val scaleHeight = mBitHeight.toFloat() / height
            // 取得想要缩放的matrix参数
            val matrix = Matrix()
            matrix.postScale(scaleWidth, scaleHeight)
            // 得到新的图片
            mClearBitmap = Bitmap.createBitmap(mClearBitmap!!, 0, 0, width, height, matrix, true)
            hasScale = true
        }
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mWidth = w
        mHeight = h

        //第一次进来的时候对图片进行处理
        deal()

    }

    companion object {
        private val DEFAUT_SCALE = 0.5f
    }
}